package cn.itcast.bos.service.transit.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.transit.SignInfoRepository;
import cn.itcast.bos.dao.transit.TransitInfoRepository;
import cn.itcast.bos.domain.transit.SignInfo;
import cn.itcast.bos.domain.transit.TransitInfo;
import cn.itcast.bos.index.WayBillIndexRepository;
import cn.itcast.bos.service.transit.SignInfoService;

@Service
@Transactional
public class SignInfoServiceImpl implements SignInfoService {

	@Autowired
	private SignInfoRepository signInfoRepository;

	@Autowired
	private TransitInfoRepository transitInfoRepository;
	
	@Autowired
	private WayBillIndexRepository wayBillIndexRepository;

	@Override
	public void save(String transitInfoId, SignInfo signInfo) {
		signInfoRepository.save(signInfo);

		// 查找运输记录
		TransitInfo transitInfo = transitInfoRepository
				.findOne(Integer.parseInt(transitInfoId));
		//将签收信息关联到运输记录上
		transitInfo.setSignInfo(signInfo);
		
		//根据signInfo的状态来对运输信息的状态进行更改(可能正常签收,也可能是出现异常)
		if("正常".equals(signInfo.getSignType())){
			//更改运输信息的状态
			transitInfo.setStatus("正常签收");
			//更改运单状态
			transitInfo.getWayBill().setSignStatus(3);
			//更改索引库
			wayBillIndexRepository.save(transitInfo.getWayBill());
		}else{
			//出现异常
			transitInfo.setStatus("异常");
			//更改运单状态
			transitInfo.getWayBill().setSignStatus(4);
			//更改索引库
			wayBillIndexRepository.save(transitInfo.getWayBill());
		}
	}

}
